package com.manlitech.manlichat.base.netty.initializer.ssl;

import com.manlitech.manlichat.base.netty.config.NettySslConfig;
import com.manlitech.manlichat.util.SslUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;

/**
 * netty ssl
 * @author Shensg
 */
@Component
public class NettySsl {

    @Autowired
    NettySslConfig nettySslConfig;

    /*private static NettySsl nettySsl ;

    private NettySsl(){

    }

    public static synchronized NettySsl getInstance(){
        if(nettySsl == null){
            nettySsl = new NettySsl();
        }
        return nettySsl ;
    }*/

    public SSLEngine getSslEngine(){
        // SSL处理器
        try {
            SSLContext sslContext = SslUtil.createSSLContext(nettySslConfig.type,
                    nettySslConfig.path, nettySslConfig.password);
            SSLEngine sslEngine = sslContext.createSSLEngine();
            sslEngine.setNeedClientAuth(false);
            sslEngine.setUseClientMode(false);
            return sslEngine;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
